Page({

  /**
   * 页面的初始数据
   */
  data: {
      contact_tel: "",
      show_customer_service: 0,
      user_info_show: false,
      user_bind_show: false,
      show_tip: false,
      tips: "敬请期待"
      //user_center_bg: "/images/img-user-bg.png",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { getApp().page.onLoad(this, options);
  },

  loadData: function (options) {
      var self = this;
      self.setData({
          store: getApp().core.getStorageSync(getApp().const.STORE),
      });
      getApp().request({
          url: getApp().api.user.index,
          success: function (res) {
              if (res.code == 0) {
                  if(self.data.__platform=='my'){
                      var menus = res.data.menus;
                      menus.forEach(function(item,index,array){
                          if(item.id==='bangding'){
                              res.data.menus.splice(index,1,0);
                          }
                      });
                  }
                  self.setData(res.data);
                  getApp().core.setStorageSync(getApp().const.PAGES_USER_USER, res.data);
                  getApp().core.setStorageSync(getApp().const.SHARE_SETTING, res.data.share_setting);
                  getApp().core.setStorageSync(getApp().const.USER_INFO, res.data.user_info);
            
    
                  //检测会员升级
                  var my_access_token = getApp().core.getStorageSync(getApp().const.ACCESS_TOKEN);
                  self.member_upgrade(my_access_token);
            

                if (!res.data.user_info.nickname || !res.data.user_info.avatar_url) {
                    wx.showToast({
                        title: '请先完善头像昵称信息'
                        , icon: 'none'
                    });
                    setTimeout(function () {
                        wx.navigateTo({
                            url: '/pages/nickavar/nickavar'
                        });
                    }, 1500);
                    return false;
                }




                  //没有绑定授权，强制授权
                  if(!res.data.user_info.binding ||  res.data.user_info.nickname == '匿名'){
                    self.user_info_show();
                  }
              }
          }
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (options) { getApp().page.onReady(this);

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) { getApp().page.onShow(this);
      var self = this;
      self.loadData();
  },

  callTel: function (e) {
      var tel = e.currentTarget.dataset.tel;
      getApp().core.makePhoneCall({
          phoneNumber: tel, //仅为示例，并非真实的电话号码
      });
  },
  apply: function (e) {
      var self = this;
      var share_setting = getApp().core.getStorageSync(getApp().const.SHARE_SETTING);
      var user_info = getApp().getUser();
      if (share_setting.share_condition == 1) {
          getApp().core.navigateTo({
              url: '/pages/add-share/index',
          })
      } else if (share_setting.share_condition == 0 || share_setting.share_condition == 2) {
          if (user_info.is_distributor == 0) {
              getApp().core.showModal({
                  title: "申请成为" + self.data.store.share_custom_data.words.share_name.name,
                  content: "是否申请？",
                  success: function (r) {
                      if (r.confirm) {
                          getApp().core.showLoading({
                              title: "正在加载",
                              mask: true,
                          });
                          getApp().request({
                              url: getApp().api.share.join,
                              method: "POST",
                              data: {
                                  form_id: e.detail.formId
                              },
                              success: function (res) {
                                  if (res.code == 0) {
                                      if (share_setting.share_condition == 0) {
                                          user_info.is_distributor = 2;
                                          getApp().core.navigateTo({
                                              url: '/pages/add-share/index',
                                          })
                                      } else {
                                          user_info.is_distributor = 1;
                                          getApp().core.navigateTo({
                                              url: '/pages/share/index',
                                          })
                                      }
                                      getApp().core.setStorageSync(getApp().const.USER_INFO, user_info);
                                  }
                              },
                              complete: function () {
                                  getApp().core.hideLoading();
                              }
                          });
                      }
                  },
              })
          } else {
              getApp().core.navigateTo({
                  url: '/pages/add-share/index',
              })
          }
      }
  },
  verify: function (e) {
      getApp().core.scanCode({
          onlyFromCamera: false,
          success: function (res) {
              getApp().core.navigateTo({
                  url: '/' + res.path,
              })
          }, fail: function (e) {
              getApp().core.showToast({
                  title: '失败'
              });
          }
      });
  },
  member: function () {
      getApp().core.navigateTo({
          url: '/pages/member/member',
      })
  },
  // 跳转积分商城
  integral_mall: function (e) {
      function isInArray(arr, val) {
          var testStr = ',' + arr.join(",") + ",";
          return testStr.indexOf("," + val + ",") != -1;
      }

      var plugin_id = 'integralmall';
      if (getApp().permission_list && getApp().permission_list.length && isInArray(getApp().permission_list, plugin_id)) {
          getApp().core.navigateTo({
              url: '/pages/share/index',
          });
      }
  },

  clearCache: function () {
      wx.showActionSheet({
          itemList: [
              '清除缓存',
          ],
          success(res) {
              if (res.tapIndex === 0) {
                  wx.showLoading({
                      title: '清除中...',
                      
                  })
                  var a = getApp().getStoreData();
                  setInterval(function(){
                      wx.hideLoading();
                  }, 1000)
              }
          }
      })
  },

getUserProfile: function (e) {
    /*getApp().core.showLoading({
        title: '加载中',
    })*/
    var self = this
    wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
            console.log(44444444444)
            getApp().core.login({
                success: function (login_res) {
                    var code = login_res.code;
                    getApp().request({
                        url: getApp().api.passport.login,
                        method: 'POST',
                        data: {
                            code: code,
                            user_info: res.rawData,
                            encrypted_data: res.encryptedData,
                            iv: res.iv,
                            signature: res.signature
                        },
                        success: function (res) {
                            getApp().core.hideLoading()
                            if (res.code === 0) {
                                /*getApp().core.showToast({
                                    title: '绑定成功,请稍等...',
                                    icon: 'none'
                                })*/
                                self.setData({
                                    user_info_show: false
                                })
                                self.bind()
                            } else {
                                getApp().core.showToast({
                                    title: '服务器出错，请再次点击绑定',
                                    icon: 'none'
                                })
                            }
                        }

                    })
                }
            })
        },
        complete:function(){
            self.setData({
                user_info_show: false
            })
        }
        
    })
},

  user_info_show: function () {
      if (this.data.user_info.binding) {
        wx.navigateTo({
            url: '/pages/nickavar/nickavar'
        });

      } else {
        this.setData({
            user_info_show: true
        });
      }

    },

    bind: function () {
        console.log(111);
          this.setData({
              user_bind_show: true
          })
      },

    //会员升级  
    member_upgrade: function(my_access_token){
        console.log(222222222222222);
        if(my_access_token){
            getApp().request({
                url: 'https://wxapp.yzhihe.com/wxappweb/member_upgrade.html?my_access_token='+my_access_token,
                success: function (res) {
                    console.log(res);
                }
            });
        }
    
    },
    show_tip: function(e){
        var self = this;
        self.setData({
            show_tip: true,
            tips: e.target.dataset.text
        })
        setTimeout(function(){
            self.setData({
                show_tip: false,
                tips: e.target.dataset.text
            })
        },1000)
    },

    test_subscribe_message: function () {
        var that = this;
        var my_access_token = getApp().core.getStorageSync(getApp().const.ACCESS_TOKEN);
        that.subscribe_message(my_access_token);
    },
    //发送订阅消息
    subscribe_message: function (my_access_token) {
    var that = this;
    //订阅消息模板id
    var template_id ="CyyDEBB0BZa8a7oCzLeOFE6HTQ2gPk2sIa-5s8JMwqs";
    wx.requestSubscribeMessage({
      tmplIds: ['CyyDEBB0BZa8a7oCzLeOFE6HTQ2gPk2sIa-5s8JMwqs'],
      success(res) {
        var res_str = JSON.stringify(res);
        if(res_str.indexOf("accept") != -1 ){  
        getApp().request({
            url: 'https://wxapp.yzhihe.com/wxappweb/subscribemessage.html?my_access_token='+my_access_token,
            data:{
                template_id: template_id,
                message_type: 'test',
            },
            success: function(res) {
                console.log("订阅成功");
                console.log(res);
            },
            fail: function(res) {
                console.log("订阅失败");
            }
        })
    }else{
        console.log(res_str)
    }
      }
    })
  }


    , zz_toApply56() {
        wx.navigateTo({
            url: '/pages/apply_discount/index'
        });
    }




});